package io.xxx.omni.osc.domain

import com.alibaba.fastjson.JSON
import io.xxx.omni.osc.common.dateTimeFormatPattern
import org.hibernate.Hibernate
import org.springframework.data.jpa.repository.JpaRepository
import java.time.LocalDateTime
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id

/**
 * 任务类型
 */
enum class JobType(val id: String, val description: String) {
    SYNC("sync", "数据同步任务")
}

/**
 * 任务信息
 */
@Entity
data class Job(
    @Id
    val id: String,
    val `class`: String,
    val name: String,
    val enabled: Boolean,
    val created: LocalDateTime,
    val modified: LocalDateTime,
) {

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other == null || Hibernate.getClass(this) != Hibernate.getClass(other)) return false
        other as Job
        return id == other.id
    }

    override fun hashCode(): Int = 615373742

    override fun toString(): String {
        return JSON.toJSONString(this)
    }
}

interface JobRepository : JpaRepository<Job, String>

/**
 * 通过任务ID记录任务的最后一次运行时间
 */
@Entity
data class JobCheckpoint(
    @Id
    val id: String,
    /**
     * 表示最后一次任务的区间截止时间，在任务配置时指定
     */
    var endTime: LocalDateTime,
    /**
     * 表示任务第一次执行的时间
     */
    var created: LocalDateTime,
    /**
     * 表示任务最后一次执行成功的时间
     */
    var modified: LocalDateTime,
) {
    constructor(id: String, endTime: LocalDateTime, created: LocalDateTime) : this(id, endTime, created, created)

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other == null || Hibernate.getClass(this) != Hibernate.getClass(other)) return false
        other as JobCheckpoint
        return id == other.id
    }

    override fun hashCode(): Int = 615373728

    override fun toString(): String {
        return JSON.toJSONStringWithDateFormat(this, dateTimeFormatPattern)
    }

}

interface JobCheckpointRepository : JpaRepository<JobCheckpoint, String>

@Entity
data class JobProperty(
    @Id
    val id: String,
    val enabled: Boolean,
    val created: LocalDateTime,
    val modified: LocalDateTime,
) {
    constructor(id: String, created: LocalDateTime) : this(id, true, created, created)

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other == null || Hibernate.getClass(this) != Hibernate.getClass(other)) return false
        other as JobProperty

        return id == other.id
    }

    override fun hashCode(): Int = 0

    @Override
    override fun toString(): String {
        return this::class.simpleName + "(id = $id )"
    }
}

interface JobPropertyRepository : JpaRepository<JobProperty, String>

/**
 * 平台任务关系
 */
@Entity
data class PlatformJob(
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    val id: Int,
    val pid: String,
    val jid: String,
    val jobClass: String,
    val enabled: Boolean,
) {
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other == null || Hibernate.getClass(this) != Hibernate.getClass(other)) return false
        other as PlatformJob
        return id == other.id || (pid == other.pid && jid == other.jid)
    }

    override fun hashCode(): Int = 1995566800

    override fun toString(): String {
        return JSON.toJSONString(this)
    }
}

interface PlatformJobRepository : JpaRepository<PlatformJob, Int>

/**
 * 店铺任务关系，
 * 根据店铺数据自动生成，如果店铺信息发生变化时将自动更新
 * @see PlatformJob
 */
@Entity
data class StoreJob(
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    val id: Int,
    val sid: String,
    val jid: String,
    /**
     * 任务最后一次执行的时间，在任务配置时指定
     */
    var endTime: LocalDateTime,
    val enabled: Boolean,
) {
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other == null || Hibernate.getClass(this) != Hibernate.getClass(other)) return false
        other as StoreJob
        return id == other.id || (sid == other.sid && jid == other.jid)
    }

    override fun hashCode(): Int = 0

    override fun toString(): String {
        return JSON.toJSONStringWithDateFormat(this, dateTimeFormatPattern)
    }
}

interface StoreJobRepository : JpaRepository<StoreJob, Int>